<template>
    <div class="BODY">
        <div class="title">文章管理</div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>
                
                <el-input v-model="queryContent" placeholder="请输入搜索内容" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>     
        </div>
        <el-table
            :data="easyTableData"
            border
            class="table"
            ref="multipleTable"
            header-cell-class-name="table-header"
            @selection-change="handleSelectionChange">
                <!-- checkbox -->
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <!-- 篇名 -->
                <el-table-column prop="id" label="篇名" align="center"></el-table-column>
                <!-- 日期 -->
                <el-table-column prop="name" label="日期"></el-table-column>
                <!-- 操作 -->
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <!-- 查看 -->
                        <el-button
                            type="text"
                            icon="el-icon-search"
                            @click="watchEdit(scope.$index, tableData)"
                        >查看</el-button>
                        <!-- 修改 -->
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            class="yellow"
                            @click="handleEdit(scope.$index, scope.row)"
                        >修改</el-button>
                        <!-- 删除 -->
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
        </el-table>
        <!-- 分页 -->
        <!-- 还有小瑕疵，API太多了还在消化 -->
        <div class="pagination">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page="query.pageIndex"
                :page-size="query.pageSize"
                :total="pageTotal"
                @current-change="handlePageChange"
            ></el-pagination>
        </div>
        <!-- </div> -->
    </div>
</template>

<script>
// import { fetchData } from '../../api/index';
import qs from 'qs';

export default {
    name: 'basetable',
    data() {
        return {
            query: {
                address: '',
                name: '',
                username: '',
                pageIndex: 1,
                pageSize: 10,
            },
            easyTableData: [
                {
                    id: 0,
                    name: '',
                    college: '',
                    grade: '',
                    major: '',
                    dormitory: ''
                }
            ],
            tableData: 
                {
                    stu_num: '03161129',
                    stu_name: 'name',
                    stu_gender:'male',
                    stu_pinyin:'wang yi fan',
                    stu_id:'420381***',
                    stu_year:'2017',
                    stu_department:'计算机科学与技术学院',
                    stu_class:'信安17-4班',
                    stu_nationality:'汉族',
                    stu_academic_record:'本科',
                    graduated_school:"丹江口市第一中学",
                    address:'湖北省十堰市丹江口市',
                    dormitory:'T2B216',
                    email:'406917347@qq.com',
                    phone:'13225237707',
                    awards:'无',
                    emergency_callee:'13117100257',
                    emergency_phone:'13197296515',
                    financial_aid:'否',
                    mental_level:'无',
                    counsellor_name:'孙凌杉',
                    counsellor_nun:'5776',
                    counsellor_phone:'13805202273/83591715',
                    comments:'无'
                },
            multipleSelection: [],
            delList: [],
            editVisible: false,
            form: {},
            idx: -1,
            id: -1,
            grade: [
                {
                    id: 1,
                    name: 2016
                }
            ],
            college: [
                {
                    id:1,
                    name:'安全工程学院'
                },
                
              
            ],
            dormFloor:[
                {
                    id:1,
                    name:'全部'
                },

            ],
            systemId: this.$route.params.systemId,
            isMajor: false,
            selectedCollege: '',
            queryContent: '',
        };
    },
    computed: {
        pageTotal () {
            let list = this.tableData
            let temp = list.length
            console.log(temp)
            return temp            
        },
    },
    watch: {
        selectedCollege (newVal) {
        // selectedCollege (newVal, oldVal) {
            if (newVal) {
                // console.log(newVal)
                // 根据id获取学院名
                let college = ''
                this.college.forEach(item => {
                    item.id === newVal ? college=item.name : ''
                })
                console.log(college);
                // 请求专业列表
                this.$http.get("https://xyt-wx.cumt.edu.cn:80/info/majors?department=" + college, {}, {withCredentials: true})
                    .then((response) => {
                        if ( response.code === 0) {
                            // 获取专业列表
                            this.major = response.data.data
                        }
                    })
            }
        }
    },
    mounted () {
        this.$http.get("https://xyt-wx.cumt.edu.cn:80/info/brief-info", {withCredentials: true})
            .then((response) => {
            if ( response.data.code === 0) {
                this.easyTableData = response.data.data
            }
        })
    },
    methods: {
        // 查看操作
        watchEdit (index, rows) {
            console.log(index);
            console.log(rows);
            let num = rows.stu_num
            // alter(rows.stu_num)
            // console.log(id)
            this.$router.push({
                path: 'https://xyt-wx.cumt.edu.cn:80/student', query: { id: num } 
            })
        },
        // 获取 easy-mock 的模拟数据
        // getData() {
        //     fetchData(this.query).then(res => {
        //         this.tableData = res.list;
        //         this.pageTotal = res.pageTotal || 50;
        //     });
        // },
        // 触发搜索按钮
        handleSearch() {
            // console.log(this.queryContent)
            this.$http.get("https://xyt-wx.cumt.edu.cn:80/info/fuzz-query?query=" + this.queryContent + "&page=" + this.page, {
                withCredentials: true
            })
            .then((response) => {
                if ( response.code === 0) {
                    // 获取简略学生列表
                    this.easyTableData = response.data.data
                }
            })
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index) {
        // handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            if (this.systemId === 0) {
                alert('学生无法修改学生信息')
            } else {
                this.idx = index;
                this.form = row;
                this.editVisible = true;
            }
        },
        // 保存编辑
        saveEdit() {
            let data = this.tableData
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
            this.$http.post('https://xyt-wx.cumt.edu.cn:80/info/update', qs.stringify(data), {withCredentials: true})
                .then((response) => {
                    if (response.data.code === 0) {
                        console.log('修改成功！')
                    } else {
                        alert("ERROR SUBMIT")
                        return false
                    }
                })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped lang='scss'>
.BODY{
    margin-left: 155px;
    margin-right:20px
}

 .title{
            text-align: center;
            margin-top: 40px;
            margin-bottom: 40px;
            font-size:25px;
            font-weight: bold;
        }
.handle-box {
    margin-bottom: 20px;
}
.el-input__inner {
    width: none;
}
.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.yellow {
    color: #E6A23C;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
label {
    font-size: 10px;
    font-family: 微软雅黑;
    font-weight: bold;
    letter-spacing: 1px;
    color: color
}
.el-form-item {
    margin: 7px 0 0;
}
</style>
